﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Entity;
using LotteryVote.Logic;
using LotteryVote.Utility;

namespace LotteryVote
{
    public partial class CompanyManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRegion();
                BindCompanyType();
                BindCompany();
            }
        }

        void BindRegion()
        {
            ddl_region.DataSource = Enum.GetNames(typeof(Region));
            ddl_region.DataBind();
        }
        void BindCompanyType()
        {
            CompanyTypeFacade comTypeFacade = new CompanyTypeFacade();
            ddl_companyType.DataSource = comTypeFacade.GetAllType();
            ddl_companyType.DataTextField = CompanyTypeModel.TYPENAME;
            ddl_companyType.DataValueField = CompanyTypeModel.ID;
            ddl_companyType.DataBind();
        }
        void BindCompany()
        {
            CompanyFacade comFacade = new CompanyFacade();
            var companyList = comFacade.GetAllCompany();
            ddl_company.DataSource = companyList;
            ddl_company.DataTextField = LotteryCompany.NAME;
            ddl_company.DataValueField = LotteryCompany.COMPANYID;
            ddl_company.DataBind();

            ddl_dayOfWeek.DataSource = Enum.GetNames(typeof(DayOfWeek));
            ddl_dayOfWeek.DataBind();
        }

        protected void btn_AddCompany_Click(object sender, EventArgs e)
        {
            CompanyFacade comFacade = new CompanyFacade();
            if (comFacade.ExistCompany(txt_Name.Text))
            {
                ltl_message.Text = "存在该公司";
                return;
            }
            comFacade.Add(txt_Name.Text, txt_Abbreviation.Text, int.Parse(ddl_companyType.SelectedValue), EnumHelper.GetEnum<Region>(ddl_region.SelectedItem.Text),
                TimeSpan.Parse(txt_opentime.Text), TimeSpan.Parse(txt_closeTime.Text));
            ltl_message.Text = "添加成功!";
        }

        protected void btn_DelById_Click(object sender, EventArgs e)
        {
            CompanyFacade comFacade = new CompanyFacade();
            comFacade.Remove(int.Parse(txt_Id.Text));
            ltl_message.Text = "删除成功!";
        }

        protected void btn_DelByName_Click(object sender, EventArgs e)
        {
            CompanyFacade comFacade = new CompanyFacade();
            comFacade.Remove(txt_Name.Text);
            ltl_message.Text = "删除成功!";
        }

        protected void btn_Update_Click(object sender, EventArgs e)
        {
            CompanyFacade comFacade = new CompanyFacade();
            comFacade.Update(int.Parse(txt_Id.Text), txt_Name.Text, txt_Abbreviation.Text, int.Parse(ddl_companyType.SelectedValue),
                EnumHelper.GetEnum<Region>(ddl_region.SelectedItem.Text),
                TimeSpan.Parse(txt_opentime.Text), TimeSpan.Parse(txt_closeTime.Text));
            ltl_message.Text = "修改成功!";
        }

        protected void btn_GetAllCompany_Click(object sender, EventArgs e)
        {
            CompanyFacade comFacade = new CompanyFacade();
            var companyList = comFacade.GetAllCompany();
            gv_companys.DataSource = companyList;
            gv_companys.DataBind();
        }

        protected void btn_addCycle_Click(object sender, EventArgs e)
        {
            CompanyLotteryCycleFacade cycleFacade = new CompanyLotteryCycleFacade();
            cycleFacade.AddCycle(int.Parse(ddl_company.SelectedValue), EnumHelper.GetEnum<DayOfWeek>(ddl_dayOfWeek.SelectedItem.Text));
        }

        protected void btn_updateToday_Click(object sender, EventArgs e)
        {
            TodayLotteryCompany.Instance.UpdateTodayLotteryCompany();
        }
    }
}